package cn.liukuan.jps.parse;

import cn.liukuan.jps.run.PSearchRun;
import cn.liukuan.jps.utils.JavassistUtils;
import cn.liukuan.jps.utils.LoggerUtils;
import cn.liukuan.jps.utils.WebClientUtils;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.slf4j.Logger;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

/**
 * Created by liukuan on 16/5/29.
 */
public class OOParser {
    
    public static Logger logger = LoggerUtils.getLogger();

    public static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();

//    static {
//        try{
//            JavassistUtils.addInterface("org.apache.lucene.store.BaseDirectory", "java.io.Serializable");
//            JavassistUtils.addInterface("org.apache.lucene.store.SingleInstanceLockFactory", "java.io.Serializable");
//            JavassistUtils.addInterface("org.apache.lucene.store.RAMFile", "java.io.Serializable");
//            JavassistUtils.addInterface("redis.clients.jedis.Jedis", "java.io.Serializable");
//            JavassistUtils.replaceFlatMap("org.apache.lucene.store.RAMDirectory", "fileMap");
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) throws IOException {

        String homePage = "http://www.open-open.com";

        OOParser ooParser = new OOParser();

        Set<String> secondaryTag = ooParser.getSecondaryTag(homePage);

        new Thread(new Runnable() {
            public void run() {

                Boolean flag = false;

                ExecutorService executorService = Executors.newFixedThreadPool(50);
                while(!flag){
                    try {
                            String href = queue.poll(2, TimeUnit.MINUTES);

                            executorService.execute(new PSearchRun(href, queue));

                    } catch (InterruptedException e) {
                        flag = true;
                        System.err.println("this process is over...");
                    }
                }


            }
        }).start();

        ooParser.getDetailTag(secondaryTag, homePage);



    }


//    public static void searchTest(){
//        try{
//            IndexReader reader = DirectoryReader.open(new RAMDirectory());
//            IndexSearcher searcher = new IndexSearcher(reader);
//            Analyzer analyzer = new IKAnalyzer();
//
//            QueryParser parser = new QueryParser("title", analyzer);
//            Query query = parser.parse("经验");
////        Query query = new TermQuery(new Term("name", "b.txt"));
//
////        Query query = new TermRangeQuery("name", new BytesRef("a"), new BytesRef("z"), true, true);
////        Query query = new TermRangeQuery("title", new BytesRef("a"), new BytesRef("z"), true, true);
////        Query query = LegacyNumericRangeQuery.newIntRange("num", 1, 3, true , true);
////        Query query = LongPoint.newRangeQuery("num", 0, 6);
//            TopDocs search = searcher.search(query, 100);
//
//            ScoreDoc[] scoreDocs = search.scoreDocs;
//
//            System.out.println(search.totalHits);
//
//            for (ScoreDoc doc : scoreDocs){
//                Document document = searcher.doc(doc.doc);
//                System.out.println(document.get("title"));
//                System.out.println(document.get("content"));
//                System.out.println(document.get("path"));
//            }
//
//            reader.close();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }


    public void getDetailTag(Set<String> set, String homePage) throws IOException {

        for (String s : set) {

            WebClient webClient = WebClientUtils.getWebClient();

            Boolean flag = false;
            int i = 0;
                try {
                    while(!flag) {
                        try {

                            System.out.println("parse url " + (i == 0? s: s + "?pn=" + i));

                            HtmlPage page = webClient.getPage(i == 0? s: s + "?pn=" + i);


                            List<HtmlAnchor> anchors = page.getAnchors();

                            for (HtmlAnchor anchor : anchors) {

                                String hrefAttribute = anchor.getHrefAttribute();

                                if (hrefAttribute.endsWith(".html")) {

                                    queue.put(homePage + hrefAttribute);
//                                    System.out.println(homePage + hrefAttribute);

                                }
                            }
                            System.out.println("url " + s + " is parsed.");
                            i++;
                        }catch (Exception e){
                            e.printStackTrace();
                            logger.debug("this's url is over");
                            flag = true;
                        }
                    }
            }catch (Exception e){

                logger.debug("url parse failed ...{}", s);
            }

        }
    }


    public Set<String> getSecondaryTag(String homePage) throws IOException {

        Set<String> result = new HashSet<String>();

        WebClient webClient = WebClientUtils.getWebClient();

        HtmlPage page = webClient.getPage(homePage+"/lib");

        List<HtmlAnchor> anchors = page.getAnchors();

        for (HtmlAnchor anchor : anchors) {
            String rel = anchor.getAttribute("rel");
            if("tag".equals(rel)){
                result.add(homePage + anchor.getHrefAttribute());
            }
        }

        return result;
    }


}
